package rs.etf.kdp.client;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;
import java.util.Properties;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

/**
 * @author Djordje Vukovic
 * @version 1.0
 * @created 16-May-2011 4:59:08 PM
 */
public class ClientContext {

	private String identifier;
	private long logicTime;
	private Object typeOfSimulation;
	private File fcomponents, fconnections, fresults;
	Socket server;
	int port;
	String address;
	private Connections connections;
	private Components components;
	JTextArea log;
	GeneralJobsManager manager;
	DefaultListModel list;

	public ClientContext(File components, File connections, File results, long logicTime, Object type, JTextArea l, DefaultListModel list, GeneralJobsManager m){
		try {
			init(components, connections, results, logicTime, type);
			log = l;
			manager = m;
			this.list = list;
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Unable to load components or connections file", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	void init(File fcomponents, File fconnections, File fresults, long logicTime, Object type) throws IOException
	{
		this.fcomponents = fcomponents;
		this.fconnections = fconnections;
		this.fresults = fresults;
		typeOfSimulation = type;
		this.logicTime = logicTime;
		
		Properties data = new Properties();
		
		try {
			data.load(new FileReader("res/data.properties"));
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "Unable to load property file. You will connect to \n host: 127.0.0.1\nport:888", "Error", JOptionPane.WARNING_MESSAGE);
			identifier = "UNKNOWN";
			address = "127.0.0.1";
			port = 888;
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		identifier = data.getProperty("name");
		String portString = data.getProperty("port");
		port = Integer.parseInt(portString);
		address = data.getProperty("address");
		
		components = new Components();
		components.loadFromFile(fcomponents);
		
		connections = new Connections();
		connections.loadFromFile(fconnections);
		
		
	}
	
	public Job generateJobFromCurrentContext()
	{
		Job generated = new Job(this);
		return generated;
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public long getLogicTime() {
		return logicTime;
	}

	public void setLogicTime(long logicTime) {
		this.logicTime = logicTime;
	}

	public Object getTypeOfSimulation() {
		return typeOfSimulation;
	}

	public void setTypeOfSimulation(Object typeOfSimulation) {
		this.typeOfSimulation = typeOfSimulation;
	}

	public File getFcomponents() {
		return fcomponents;
	}

	public void setFcomponents(File fcomponents) {
		this.fcomponents = fcomponents;
	}

	public File getFconnections() {
		return fconnections;
	}

	public void setFconnections(File fconnections) {
		this.fconnections = fconnections;
	}

	public File getFresults() {
		return fresults;
	}

	public void setFresults(File fresults) {
		this.fresults = fresults;
	}

	public Socket getServer() {
		return server;
	}

	public void setServer(Socket server) {
		this.server = server;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Connections getConnections() {
		return connections;
	}

	public void setConnections(Connections connections) {
		this.connections = connections;
	}

	public Components getComponents() {
		return components;
	}

	public void setComponents(Components components) {
		this.components = components;
	}

	public GeneralJobsManager getManager() {
		return manager;
	}

	public void setManager(GeneralJobsManager manager) {
		this.manager = manager;
	}

	public DefaultListModel getList() {
		return list;
	}

	public void setList(DefaultListModel list) {
		this.list = list;
	}
	
	


}